Passed
Push — main ( c4701f...95102b )
by Andrii
02:26
created

-infer.ts ➔ exclusion   A

Complexity

Conditions 2

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
dl 0
loc 27
rs 9.8
c 0
b 0
f 0
1
//@ts-nocheck 
2
3
import { ClassValue } from "../../defs"
4
5
export {}
6
7
// type Excluder<
8
//   S extends Record<string, ClassValue>,
9
//   E extends {[K in keyof S]?: ClassValue}
10
// > = { [P in Exclude<keyof S, keyof E>]: S[P]; }
11
12
// interface Exclusion<
13
//   S extends Record<string, ClassValue>
14
// > {
15
//   (source: S, ex: {[K in keyof S]?: ClassValue}): Excluder<S, typeof ex>
16
// }
17
18
function exclusion<
19
  S extends Record<string, ClassValue>,
20
>(
21
  source: S, ex: {[K in keyof S]?: ClassValue}
22
): typeof ex extends Record<infer E, any>
23
? { [P in Exclude<keyof S, E>]: S[P]; }
24
: never
25
{
26
  const $return = {...source}
27
  for (const k in ex) {
28
    delete $return[k]
29
  }
30
31
  //@ts-expect-error
32
  return $return 
33
}
34
35
const source: Record<"a"|"b"|"c"|"d"|"e", ClassValue> = {a: "a", b: undefined, c: "c", d: undefined, e: undefined}
36
37
const step0 = exclusion(
38
  source,
39
  //@ts-expect-error 'z' does not exist in type
40
  {z: undefined}
41
)
42
, answ0: typeof step0 = {
43
  //@ts-expect-error Type 'undefined' is not assignable to type 'never'
44
  whatever: undefined
45
}
46
, step1 = exclusion(source, {a: "a", b: undefined})
47
, step2 = exclusion(step1, {"c": undefined})
48
//@ts-expect-error Property 'd' is missing
49
, answ
50
: typeof step2 = {
51
  e: "",
52
  //@ts-expect-error Object literal may only specify known properties, and 'z'
53
  z: ""
54
}
55
export {step2, answ0}